var $;
layui.config({
	base : "js/"
}).use(['form','layer','jquery'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage;
		$ = layui.jquery;
		/**初始化水品牌下拉列表**/
		getBrandNameList();
 	form.on("submit(create)",function(data){
 		var dto = {
// 			"waterBrandId":$(".waterBrandId").val(),
// 			"waterBrandName": $(".waterBrandName").find("option:selected").text(),
 			"waterName": $(".waterName").find("option:selected").text(),
 			"waterId":$(".waterName").val(),
 			"waterNum": $(".waterNum").val(),
 			"remark": $(".remark").val(),
 			"billSn": $(".billSn").val()
 		};
        $.ajax({
	        url: API_URL_HEAD+"putInBills/createPutInBills",
	        type: 'post',
	        contentType: "application/json; charset=utf-8",
	        data: JSON.stringify(dto),
	        dataType: 'json',
	        success: function (data) {
	            if(data.code == 0){
	               //刷新父页面
		 			parent.location.reload();
	            }
	        }
    	});

		return false;

 	})
 	//水品牌下拉框监听
    form.on("select(waterBrandName)",function(data)
    {
    	$(".waterName").find("option:not(:first)").remove();
    	var waterBrandId = $(data.elem).val();
    	var dto = {"waterBrandId":waterBrandId};
	      $.ajax({
	      	    type: "POST",
	      	    dataType: "json",
	      	    url : API_URL_HEAD+"water/select",
	      	    contentType: "application/json; charset=utf-8",
	      	    async:true,
	      	    data:JSON.stringify(dto),
	      	  	success: function (data) {  
	      	  		if(data.code == "0"){
		      	  		var jsArr = data.data;
		      	  		var apartAreaObj = $(".waterName");
		      	  		createSelectOptions(apartAreaObj,jsArr,"waterName");
		      	  		form.render(null,"waterName");
	      	  		}
	      	    },
	      	    error: function(data) { 
	      	    	console.log(data);
	      	    	layer.alert("服务器忙，请稍后重试");
	      	    }
	      	});	
    	//根据选中的供应商ID获取水下拉列表
    });
	/*
	 *水品牌列表
	 */
	function getBrandNameList(){
		$(".waterBrandName").find("option:not(:first)").remove();
		var dto = {};
	      $.ajax({
	      	    type: "POST",
	      	    dataType: "json",
	      	    url : API_URL_HEAD+"waterBrand/select",
	      	    contentType: "application/json; charset=utf-8",
	      	    async:true,
	      	    data:JSON.stringify(dto),
	      	  	success: function (data) {  
	      	  		if(data.code == "0"){
		      	  		var jsArr = data.data;
		      	  		var apartAreaObj = $(".waterBrandName");
		      	  		createSelectOptions(apartAreaObj,jsArr,"brandName");
		      	  		form.render(null,"waterBrandName");
	      	  		}
	      	    },
	      	    error: function(data) { 
	      	    	console.log(data);
	      	    	layer.alert("服务器忙，请稍后重试");
	      	    }
	      	});	
	}
 	//生成select options
	function createSelectOptions(selObj,dataArray,name,id){
		for(var i in dataArray){
			var every = dataArray[i];
			if(id==undefined){
				$(selObj).append("<option value='"+every["id"]+"'>"+every[name]+"</option>");
			}else{
				if(every["id"]==id){
					$(selObj).append("<option value='"+every["id"]+"'  selected=\"selected\">"+every[name]+"</option>");
				}else{
					$(selObj).append("<option value='"+every["id"]+"'>"+every[name]+"</option>");
				}
			}
			
		}
	}	
})

//格式化时间
function formatTime(_time){
    var year = _time.getFullYear();
    var month = _time.getMonth()+1<10 ? "0"+(_time.getMonth()+1) : _time.getMonth()+1;
    var day = _time.getDate()<10 ? "0"+_time.getDate() : _time.getDate();
    var hour = _time.getHours()<10 ? "0"+_time.getHours() : _time.getHours();
    var minute = _time.getMinutes()<10 ? "0"+_time.getMinutes() : _time.getMinutes();
    return year+"-"+month+"-"+day+" "+hour+":"+minute;
}
